package cyberdict.scr;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;


public class DictFileList {
	//static final private String FILELIST_TAG = "[CyberDict - DictFileList]";
	public ArrayList<DictFile> items;

	public DictFileList(String dbPath, String dbExtension)
	{
		items = new ArrayList<DictFile>();
		getDictFileList(dbPath,dbExtension);
	}

	private void getDictFileList(String dbPath, String dbExtension)
	{
		items.clear();
		File dataDirectory = new File(dbPath);
		if (!dataDirectory.exists()) //Data directory doesn't exist, create it
		{
			dataDirectory.mkdirs();

		}
        FileFilter ffDir = new FileFilter()
        {
        	public boolean accept(File f)
        	{
        		return f.isDirectory();
        	}
        };

        File[] lstDirectory = dataDirectory.listFiles(ffDir);
        if (lstDirectory != null && lstDirectory.length > 0)
        {
	        for (File currentDirectory : lstDirectory)
	        {
	            DictFile db = new DictFile();
	        	String path = currentDirectory.getName();
	        	//Log.i(FILELIST_TAG,"Filelist path = " + path);
		        	
          		db.fileName = currentDirectory.getName();
          		db.path = "/"+currentDirectory.getPath();
          		//Log.i(FILELIST_TAG,"fileName = " + db.fileName + " | path = " + db.path);
	          	File ifoFile = new File(path + ".ifo");
	          	/*
          		if (ifoFile.exists())
	          	{
	          		
	          		String data;
	          		String arrData[] = null;
	          		try
	          		{
			          	BufferedReader brIfoFile = new BufferedReader(new FileReader(ifoFile));
			          	while (brIfoFile.ready())
			          	{
				          	data = brIfoFile.readLine();
				          	arrData = data.split("=");
				          	arrData[0] = arrData[0].trim();
				          	if (arrData[0].contains("name"))
				          	{
				          		db.dictName = arrData[1].trim();
				          	}
				        }
	          		}
	          		catch (Exception ex){
	          			db.dictName = db.fileName;
	          		}
		          	
	          	}
          		else*/
          		{
          			db.dictName = db.fileName;
          			
          		}
          		//add to list
          		items.add(db);
	        }
	        
        }
        
	}
}

